package com.example.appsnight;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import shared.Constants;
import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class Programa extends ListActivity {

    @SuppressLint({ "NewApi", "NewApi" })
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_programa);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        
        Intent i = getIntent();
        String programa = i.getStringExtra(Constants.EXTRA_PROGRAM);
        
		JSONArray jsonArray;
		JSONObject jsonObject;
		ArrayList<ProgramActivity> actList = new ArrayList<ProgramActivity>();
		try {
			String time = "";
			String title = ""; 
			jsonArray = new JSONArray(programa);
			for (int j = 0; j < jsonArray.length(); j++) {
				jsonObject = new JSONObject(jsonArray.getString(j));
				time = jsonObject.getString("time");
				title = jsonObject.getString("name");
				actList.add(new ProgramActivity(time, title, ""));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		setListAdapter(new ProgramAdapter(this, actList));
    }
    
    public void showToast(String string) {
		int duration = Toast.LENGTH_SHORT;
		Toast toast = Toast.makeText(this, string, duration);
		toast.show();
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_programa, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
